~ chicken-core (chicken-5) /manual/Module (chicken read-syntax)
Trap1[[tags: manual]]
2[[toc:]]
3
4== Module (chicken read-syntax)
5
6This module provides procedures which can be used to extend the reader
7with custom read syntax.
8
9=== define-reader-ctor
10
11<procedure>(define-reader-ctor SYMBOL PROC)</procedure>
12
13Define new read-time constructor for {{#,}} read syntax. For further information, see
14the documentation for [[http://srfi.schemers.org/srfi-10/srfi-10.html|SRFI-10]].
15
16
17=== set-read-syntax!
18
19<procedure>(set-read-syntax! CHAR-OR-SYMBOL PROC)</procedure>
20
21When the reader encounters the non-whitespace character {{CHAR}} while reading
22an expression from a given port, then the procedure {{PROC}} will be called with
23that port as its argument. The procedure should return a value that will be returned
24to the reader:
25
26<enscript highlight=scheme>
27 ; A simple RGB color syntax:
28
29 (set-read-syntax! #\%
30 (lambda (port)
31 (apply vector
32 (map (cut string->number <> 16)
33 (string-chop (read-string 6 port) 2) ) ) ) )
34
35 (with-input-from-string "(1 2 %f0f0f0 3)" read)
36 ; ==> (1 2 #(240 240 240) 3)
37</enscript>
38
39If {{CHAR-OR-SYMBOL}} is a symbol, then a so-called ''read-mark'' handler is defined.
40In that case the handler procedure will be called when a character-sequence of the
41form {{#!SYMBOL}} is encountered.
42
43You can undo special handling of read-syntax by passing {{#f}} as the second argument
44(if the syntax was previously defined via {{set-read-syntax!}}).
45
46As a special case, your handler can return zero values, via {{(values)}}. This causes
47the reader to completely ignore whatever input you've read, rather than returning some
48possibly unspecified value. This can be useful in macro context, reading comments,
49conditional compilation, and so forth. Available in CHICKEN 4.6.6 and later.
50
51Note that all of CHICKEN's special non-standard read-syntax is handled directly by the reader.
52To disable built-in read-syntax, define a handler that triggers an error (for example).
53
54
55=== set-sharp-read-syntax!
56
57<procedure>(set-sharp-read-syntax! CHAR-OR-SYMBOL PROC)</procedure>
58
59Similar to {{set-read-syntax!}}, but allows defining new {{#<CHAR> ...}} reader syntax.
60If the first argument is a symbol, then this procedure is equivalent to {{set-read-syntax!}}.
61
62{{PROC}} may be {{#f}} to disable previously defined "sharp" read syntax.
63
64
65=== set-parameterized-read-syntax!
66
67<procedure>(set-parameterized-read-syntax! CHAR-OR-SYMBOL PROC)</procedure>
68
69Similar to {{set-sharp-read-syntax!}}, but intended for defining reader syntax of the
70form {{#<NUMBER><CHAR> ...}}. The handler procedure {{PROC}} will be called with two
71arguments: the input port and the number preceding
72the dispatching character.
73If the first argument is a symbol, then this procedure is equivalent to {{set-read-syntax!}}.
74
75{{PROC}} may be {{#f}} to disable previously defined parameterized read syntax.
76
77
78=== copy-read-table
79
80<procedure>(copy-read-table READ-TABLE)</procedure>
81
82Returns a copy of the given read-table. You can access the currently
83active read-table with {{(current-read-table)}}. This procedure can
84be useful to restore an old read-table after temporarily introducing
85new read syntax.
86
87
88=== current-read-table
89
90<parameter>(current-read-table)</parameter>
91
92A read-table object that holds read-procedures for special non-standard
93read-syntax (see {{set-read-syntax!}} for more information).
94
95
96---
97Previous: [[Module (chicken random)]]
98
99Next: [[Module (chicken repl)]]